package com.dabaozhan.modules.wx_app.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.dabaozhan.common.exception.RRException;
import com.dabaozhan.modules.wx_app.base.ResponseDto;
import com.dabaozhan.modules.wx_app.entity.StockResponseEntity;
import com.dabaozhan.modules.wx_app.enums.ResponseCodeEnum;
import com.dabaozhan.modules.wx_app.facade.StockFacade;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "库存统计")
@RestController
@RequestMapping("/wx_app")
public class StockController {
	private static final Logger LOGGER = LoggerFactory.getLogger(StockController.class);
	
	@Autowired
	private StockFacade stockFacade;

	@ApiOperation(value = "查询库存")
    @RequestMapping(value = "/getStock")
	public String k(String userId){
		ResponseDto<List<StockResponseEntity>> responseDto = new ResponseDto<List<StockResponseEntity>>();
		try{
			List<StockResponseEntity> list = stockFacade.getStockList(userId);
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
			responseDto.setResult(list);
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("查询库存发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("查询库存请求参数:{}-返回结果:{}",userId,JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}
}
